﻿using Newtonsoft.Json;
using Request;
using Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class LoadController : Controller
    {
        [System.Web.Mvc.HttpGet]
        public ActionResult Index()
        {
            Core.AutoMapperInit.Run();

            string json = Request.QueryString.Get("q");
            a所有5个Store的请求类 loadRequest =  JsonConvert.DeserializeObject<a所有5个Store的请求类>(json);

            EF.GanttEntities db = new EF.GanttEntities();

            var loadResponse = new _b整个Gantt的5个Store响应(loadRequest.requestId);

            //if (loadRequest.calendars != null)
            //{
            //    var cal = db.Calendars.Where(c => !c.parentIdRaw.HasValue).ToList().Select(x => x.ToDto());
            //    loadResponse.setCalendars(cal, 1);
            //}
            //if (loadRequest.assignments != null)
            //{
            //    var tas = db.Assignments.ToList().Select(x => x.ToDto());
            //    loadResponse.setAssignments(tas);
            //}
            if (loadRequest.tasks != null)
            {
                var tas = db.Tasks.Where(t => !t.parentIdRaw.HasValue).OrderBy(t => t.index).ToList();
                var t2 = tas.Select(x => x.ToDto()).ToList();
                loadResponse.setTasks(t2);
            }


            //if (loadRequest.resources != null)
            //{
            //    var tas = db.Resources.ToList().Select(x => x.ToDto());
            //    loadResponse.setResources(tas);
            //}
            //if (loadRequest.dependencies != null)
            //{
            //    var tas = db.Dependencies.ToList().Select(x => x.ToDto());
            //    loadResponse.setDependencies(tas);
            //}




            loadResponse.revision = Convert.ToInt32( db.Options.Find("revision").value);

            return Content(JsonConvert.SerializeObject(loadResponse), "application/json");

        }
    }
}